//
//  SHEmoticonPageViewCell.swift
//  SHWeibo01
//
//  Created by 卫平玉 on 16/7/30.
//  Copyright © 2016年 卫平玉. All rights reserved.
//

import UIKit

class SHEmoticonPageViewCell: UICollectionViewCell {
    //保存20 个按钮
    var buttons: [SHEmoticonButton] = [SHEmoticonButton]()
    // 定义一个装有模型的数组
    var emoticons: [SHEmoticon]?{
        
        didSet{
            
            for value in buttons{
                value.hidden = true
            }
            
            for (i,emoticon) in emoticons!.enumerate(){
                
                let button = buttons[i]
                
                button.emoticon = emoticon
                
                button.hidden = false
                
                if emoticon.isEmoji{
                    
                    button.setTitle((emoticon.code ?? "" as NSString).emoji(), forState: UIControlState.Normal)
                     // 解决image和title 重复问题
                    button.setImage(nil, forState: UIControlState.Normal)
                }else{
                    
                    button.setImage(UIImage.getEmoticonImage(emoticon.path), forState: UIControlState.Normal)
                    // 解决image和title 重复问题
                    button.setTitle(nil, forState: UIControlState.Normal)
                }
                
            }
        }
    }
   
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 监听事件
    // 删除按钮点击发送通知
    func deleteButtonClick(){
        // 发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(WBEmoticonDeleteButtonNoti, object: nil)
    }
    
    func buttonClick(button: SHEmoticonButton){
        
        NSNotificationCenter.defaultCenter().postNotificationName(WBEmoticonButtonsNoti, object: button.emoticon)
    }
    
    private func setupUI(){
        
        addChildButtons()
        contentView.addSubview(deleteButton)
        
//        backgroundColor = RandomColor()
        
//        contentView.addSubview(messageLabel)
//        
//        messageLabel.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(contentView)
//        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let childW = (self.frame.size.width - 10) / CGFloat(SHEmoticonMaxCol)
        
        let childH = (self.frame.size.height - 20) / CGFloat(SHEmoticonMaxRow)
        
        for (i,value) in buttons.enumerate(){
            
            let col = CGFloat(i%SHEmoticonMaxCol)
            
            let row = CGFloat(i/SHEmoticonMaxCol)
            
            value.frame = CGRectMake(childW*col+5, childH*row, childW, childH)
        }
        
        deleteButton.frame = CGRectMake(self.frame.size.width-childW-5, childH*2, childW, childH)
       
    }
    
    func addChildButtons(){
        
        for _ in 0..<SHEmoticonMaxCount{
            let button = SHEmoticonButton()
            
            button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
            button.titleLabel?.font = UIFont.systemFontOfSize(30)

            contentView.addSubview(button)
            buttons.append(button)
        }
    }
    
    private lazy var messageLabel: UILabel = {
        
        let lab = UILabel(textColor: UIColor.redColor(), textFont: 35, maxWidth: SCREENW)
        lab.textAlignment = .Center
        
        return lab
    }()
    
    private lazy var deleteButton: UIButton = {
       
        let button = UIButton()
        
        button.addTarget(self, action: "deleteButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        
        return button
    }()
    
}
